package cn.tedu.weibo2.service.impl;

import cn.tedu.weibo2.exception.ServiceException;
import cn.tedu.weibo2.response.StatusCode;
import cn.tedu.weibo2.mapper.UserMapper;
import cn.tedu.weibo2.pojo.dto.UserRegDTO;
import cn.tedu.weibo2.pojo.entity.User;
import cn.tedu.weibo2.pojo.vo.UserVO;
import cn.tedu.weibo2.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public void reg(UserRegDTO userRegDTO) {
       UserVO userVO=userMapper.selectByUsername(userRegDTO.getUsername());
       if (userVO!=null){
           throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS);
       }
        User user=new User();
        BeanUtils.copyProperties(userRegDTO,user);
        user.setCreated(new Date());
        userMapper.insert(user);
    }
}
